package com.jiazhong.basic.pack0316.thread.work01;

import java.util.ArrayList;
import java.util.List;

public class Train {

    private List<String> tickets = new ArrayList<>();

    public Train() {
        for (int i = 1; i < 101; i++) {
            tickets.add("票号：" + i);
        }
    }

    public synchronized String buy() {
        if (tickets.isEmpty()) {
            return null;
        }
        int index = (int) (Math.random() * tickets.size());
        // 获取到票
        String ticket = tickets.get(index);
        // 删除票
        tickets.remove(index);
        return ticket;
    }
}
